package br.com.localfloripa.controllers;

import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;

import br.com.localfloripa.dao.ClienteDao;
import br.com.localfloripa.dao.EstadoDao;
import br.com.localfloripa.dao.MunicipioDao;
import br.com.localfloripa.entities.Cliente;
import br.com.localfloripa.entities.TipoNegociacao;

@Named("templateController")
@Scope("request")
public class TemplateController implements Serializable {

	private static final long serialVersionUID = -8216358187032931426L;
	private static final Logger log = LoggerFactory.getLogger(TemplateController.class);
	
	@Inject
	@Named("clienteDao")
	private ClienteDao clienteDao;
	
	@Inject
	@Named("searchImovelController")
	private SearchImovelController searchController;
	
	@Inject
	@Named("municipioDao")
	private MunicipioDao municipioDao;
	
	@Inject
	@Named("estadoDao")
	private EstadoDao estadoDao;
	
	public TemplateController() {		
		
	}
	
	public String loadTemplate() {
		
		try {
			Cliente cliente = null;
			String imobiliaria = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("imobiliaria");
			if (imobiliaria != null && !imobiliaria.equals("")) {
				cliente = clienteDao.findByURLTemplate(imobiliaria);
			} else {
				String idCliente = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idCliente");
				cliente =  clienteDao.findById(Long.parseLong(idCliente));
			}
					
			// carrega opção de busca
			searchController.setTipoNegociacao(TipoNegociacao.A.name());
			
			searchController.setEstados(estadoDao.listEstadosByCliente(cliente));
			if (searchController.getEstados().size() > 0) {
				searchController.setMunicipios(municipioDao.listMunicipiosByEstadoAndCliente(searchController.getEstados().get(0), cliente));
				if (searchController.getMunicipios().size() > 0)
					searchController.setBairros(municipioDao.listBairrosByMunicipioAndCliente(searchController.getMunicipios().get(0), cliente));
			}				
			searchController.setCliente(cliente);
			searchController.loadImoveis();
						
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return "/faces/site/xhtml/templates/complex-body.xhtml";
	}
	
	public String loadQuemSomos() {
		try {
			Cliente cliente = null;
			String idCliente = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idCliente");
			cliente =  clienteDao.findById(Long.parseLong(idCliente));			
			searchController.setCliente(cliente);
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}		
		
		return "/faces/site/xhtml/templates/quem-somos.xhtml";
	}
	
	public String loadLocalizacao() {
		try {
			Cliente cliente = null;
			String idCliente = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idCliente");
			cliente =  clienteDao.findById(Long.parseLong(idCliente));			
			searchController.setCliente(cliente);
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}		
		
		return "/faces/site/xhtml/templates/localizacao.xhtml";
	}
	
	public String loadContato() {
		
		try {
			Cliente cliente = null;
			String idCliente = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idCliente");
			cliente =  clienteDao.findById(Long.parseLong(idCliente));			
			searchController.setCliente(cliente);
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}	
		
		return "/faces/site/xhtml/templates/contato.xhtml";
	}
	
}
